package com.wsh.shop.back.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.wsh.shop.common.pojo.ApiResponse;
import com.wsh.shop.common.servlet.BaseServlet;
import com.wsh.shop.common.util.PageInfoUtil;
import com.wsh.shop.users.pojo.ACate;
import com.wsh.shop.users.pojo.ACateEnum;
import com.wsh.shop.users.pojo.AGoods;
import com.wsh.shop.users.service.IGoodsDbService;
import com.wsh.shop.users.service.IUsersDbService;
import com.wsh.shop.users.service.impl.GoodsDbServiceImpl;
import com.wsh.shop.users.service.impl.UsersDbServiceImpl;

/**
 * 第二个Servlet 只存储和管理员息息相关的信息 登陆后的操作
 * 
 * @author wangshMac
 */
@WebServlet(urlPatterns = "/back/GoodsBackServlet")
public class GoodsBackServlet extends BaseServlet
{
	private IGoodsDbService goodsDbService;
	private IUsersDbService usersDbService;
	
	@Override
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		UsersDbServiceImpl usersDbServiceImpl = new UsersDbServiceImpl();
		GoodsDbServiceImpl goodsDbServiceImpl = new GoodsDbServiceImpl();
		
		goodsDbServiceImpl.setUsersDbService(usersDbServiceImpl);
		usersDbServiceImpl.setGoodsDbService(goodsDbServiceImpl);
		
		this.goodsDbService = goodsDbServiceImpl ; 
		this.usersDbService = usersDbServiceImpl ; 
		
	}
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 接收参数 */
		String method = request.getParameter("method");
		if ("cateList".equalsIgnoreCase(method))
		{
			/* 分类列表 */
			cateList(request, response);
		} else if ("cateInsert".equalsIgnoreCase(method))
		{
			/* 分类添加 */
			cateInsert(request, response);
		} else if ("cateInsertSubmit".equalsIgnoreCase(method))
		{
			/* 分类添加提交 */
			cateInsertSubmit(request, response);
		} else if ("cateUpdate".equalsIgnoreCase(method))
		{
			/* 分类更新 */
			cateUpdate(request, response);
		} else if ("cateUpdateSubmit".equalsIgnoreCase(method))
		{
			/* 分类提交 */
			cateUpdateSubmit(request, response);
		} if ("goodsList".equalsIgnoreCase(method))
		{
			/* 分类列表 */
			goodsList(request, response);
		} else if ("goodsInsert".equalsIgnoreCase(method))
		{
			/* 分类添加 */
			goodsInsert(request, response);
		} else if ("goodsInsertSubmit".equalsIgnoreCase(method))
		{
			/* 分类添加提交 */
			goodsInsertSubmit(request, response);
		} else if ("goodsUpdate".equalsIgnoreCase(method))
		{
			/* 分类更新 */
			goodsUpdate(request, response);
		} else if ("goodsUpdateSubmit".equalsIgnoreCase(method))
		{
			/* 分类提交 */
			goodsUpdateSubmit(request, response);
		} 
	}

	/**
	 * 分类更新提交
	 * @param request
	 * @param response
	 */
	private void cateUpdateSubmit(HttpServletRequest request, HttpServletResponse response)
	{
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		ApiResponse<ACate> apiResponse = this.goodsDbService.findOneCateService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		ACate cate = apiResponse.getDataOneJava() ;
		
		boolean flag = false ; 
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			/* 需要返回json */
			String name = request.getParameter("name");
			String content = request.getParameter("content");
			String status = request.getParameter("status");
			String pubTime = request.getParameter("pubTime");
			
			/* 赋值 */
			cate.setName(name);
			cate.setContent(content);
			cate.setStatus(Byte.valueOf(status));
			cate.setPubTime(this.dateFormatUtil.strDateTime(pubTime));
			
			flag = true ; 
		}
		
		ApiResponse<Object> apiResponseDb = new ApiResponse<Object>();
		if(flag)
		{
			apiResponseDb = this.goodsDbService.updateOneCateService(cate);
		}
		
		/* 成功,提示 */
		JSONObject resultJSON = apiResponseDb.toJSON();
		this.printOut(resultJSON.toJSONString(), response);
	}

	/**
	 * 分类更新
	 * @param request
	 * @param response
	 */
	private void cateUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		this.path = "/WEB-INF/jsp/back/goods/cateInfo.jsp" ;
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		ApiResponse<ACate> apiResponse = this.goodsDbService.findOneCateService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			this.path = "/WEB-INF/jsp/back/goods/cateUpdate.jsp" ;
		}
		/* 页面跳转 */
		request.getRequestDispatcher(path).forward(request, response);
	}

	/**
	 * 分类添加提交
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void cateInsertSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		this.path = "/WEB-INF/jsp/back/goods/cateInsert.jsp" ;
		/* 需要返回json */
		String name = request.getParameter("name");
		String content = request.getParameter("content");
		String status = request.getParameter("status");
		String pubTime = request.getParameter("pubTime");
		
		/* 创建对象 */
		ACate cate = new ACate();
		
		/* 赋值 */
		cate.setName(name);
		cate.setContent(content);
		cate.setStatus(Byte.valueOf(status));
		cate.setPubTime(this.dateFormatUtil.strDateTime(pubTime));
		
		/* 保存数据库 */
		ApiResponse<Object> apiResponse = this.goodsDbService.saveOneCateService(cate);
		request.setAttribute("response", apiResponse.toJSON());
		/* 成功,提示 */
		JSONObject resultJSON = apiResponse.toJSON();
		this.printOut(resultJSON.toJSONString(), response);
		
	}

	/**
	 * 分类添加
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void cateInsert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		this.path = "/WEB-INF/jsp/back/goods/cateInsert.jsp" ;
		/* 页面跳转 */
		request.getRequestDispatcher(path).forward(request, response);
	}

	/**
	 * 分类列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void cateList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		this.path = "/WEB-INF/jsp/back/goods/cateList.jsp" ; 
		
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<ACate> apiResponse = this.goodsDbService.findCondListCateService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		request.getRequestDispatcher(path).forward(request, response);
	}

	/**
	 * 商品更新提交
	 * @param request
	 * @param response
	 */
	private void goodsUpdateSubmit(HttpServletRequest request, HttpServletResponse response)
	{
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		ApiResponse<AGoods> apiResponse = this.goodsDbService.findOneGoodsService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		AGoods goods = apiResponse.getDataOneJava() ;
		
		boolean flag = false ; 
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			/* 需要返回json */
			String cateId = request.getParameter("cateId");
			String title = request.getParameter("title");
			String name = request.getParameter("name");
			String brief = request.getParameter("brief");
			String price = request.getParameter("price");
			String stockNum = request.getParameter("stockNum");
			String dataType = request.getParameter("dataType");
			String content = request.getParameter("content");
			String status = request.getParameter("status");
			String pubTime = request.getParameter("pubTime");
			
			/* 赋值 */
			goods.setCateId(Integer.valueOf(cateId));
			goods.setTitle(title);
			goods.setName(name);
			goods.setBrief(brief);
			goods.setPrice(Double.valueOf(price));
			goods.setStockNum(Integer.valueOf(stockNum));
			goods.setDataType(Byte.valueOf(dataType));
			goods.setContent(content);
			goods.setStatus(Byte.valueOf(status));
			goods.setPubTime(this.dateFormatUtil.strDateTime(pubTime));
			
			flag = true ; 
		}
		
		ApiResponse<Object> apiResponseDb = new ApiResponse<Object>();
		if(flag)
		{
			apiResponseDb = this.goodsDbService.updateOneGoodsService(goods);
		}
		
		/* 成功,提示 */
		JSONObject resultJSON = apiResponseDb.toJSON();
		this.printOut(resultJSON.toJSONString(), response);
	}

	/**
	 * 商品更新
	 * @param request
	 * @param response
	 */
	private void goodsUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		this.path = "/WEB-INF/jsp/back/goods/goodsInfo.jsp" ;
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		condMap.put("extend", "true");
		ApiResponse<AGoods> apiResponse = this.goodsDbService.findOneGoodsService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			/* 查询所有的分类 */
			condMap.clear();
			condMap.put("status", ACateEnum.STATUS_ENABLE.getStatus());
			ApiResponse<ACate> cateApiResponse = this.goodsDbService.findCondListCateService(null, condMap);
			request.setAttribute("cateResponse", cateApiResponse.toJSON());
			
			this.path = "/WEB-INF/jsp/back/goods/goodsUpdate.jsp" ;
		}
		/* 页面跳转 */
		request.getRequestDispatcher(path).forward(request, response);
	}

	/**
	 * 商品添加提交
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void goodsInsertSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		this.path = "/WEB-INF/jsp/back/goods/goodsInsert.jsp" ;
		/* 需要返回json */
		String cateId = request.getParameter("cateId");
		String title = request.getParameter("title");
		String name = request.getParameter("name");
		String brief = request.getParameter("brief");
		String price = request.getParameter("price");
		String stockNum = request.getParameter("stockNum");
		String dataType = request.getParameter("dataType");
		String content = request.getParameter("content");
		String status = request.getParameter("status");
		String pubTime = request.getParameter("pubTime");
		
		/* 创建对象 */
		AGoods goods = new AGoods();
		
		/* 赋值 */
		goods.setCateId(Integer.valueOf(cateId));
		goods.setTitle(title);
		goods.setName(name);
		goods.setBrief(brief);
		goods.setPrice(Double.valueOf(price));
		goods.setStockNum(Integer.valueOf(stockNum));
		goods.setDataType(Byte.valueOf(dataType));
		goods.setContent(content);
		goods.setStatus(Byte.valueOf(status));
		goods.setPubTime(this.dateFormatUtil.strDateTime(pubTime));
		
		/* 保存数据库 */
		ApiResponse<Object> apiResponse = this.goodsDbService.saveOneGoodsService(goods);
		request.setAttribute("response", apiResponse.toJSON());
		/* 成功,提示 */
		JSONObject resultJSON = apiResponse.toJSON();
		this.printOut(resultJSON.toJSONString(), response);
		
	}

	/**
	 * 商品添加
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void goodsInsert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		this.path = "/WEB-INF/jsp/back/goods/goodsInsert.jsp" ;
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 查询所有的分类 */
		condMap.clear();
		condMap.put("status", ACateEnum.STATUS_ENABLE.getStatus());
		ApiResponse<ACate> cateApiResponse = this.goodsDbService.findCondListCateService(null, condMap);
		request.setAttribute("cateResponse", cateApiResponse.toJSON());
		/* 页面跳转 */
		request.getRequestDispatcher(path).forward(request, response);
	}

	/**
	 * 商品列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void goodsList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		this.path = "/WEB-INF/jsp/back/goods/goodsList.jsp" ; 
		
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		
		String cateId = request.getParameter("cateId");
		if(cateId == null)
		{
			cateId = "" ; 
		}
		request.setAttribute("cateId", cateId);
		condMap.put("cateId", cateId);
		
		/* 扩展信息 */
		condMap.put("extend", "true");
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<AGoods> apiResponse = this.goodsDbService.findCondListGoodsService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 查询所有的分类 */
		condMap.clear();
		condMap.put("status", ACateEnum.STATUS_ENABLE.getStatus());
		ApiResponse<ACate> cateApiResponse = this.goodsDbService.findCondListCateService(null, condMap);
		request.setAttribute("cateResponse", cateApiResponse.toJSON());
		
		/* 页面跳转 */
		request.getRequestDispatcher(path).forward(request, response);
	}
}
